import java.io.*;
import java.sql.SQLException;

public class SGResponse
{

	private String runcode, authorID, examDate;

	String[] arguments;
	PrintWriter fileOut;
	String fileOutName; 
	final String SITE_BASE_URL = "http://localhost/finalTest";
	final String PATH_TO_FILES = "/tmp/ape/";
	
	public SGResponse(String[] argumentsIn) throws SQLException, IOException
	{
		System.out.println("The length of arguments is: " + argumentsIn.length + " here!!!");
		this.arguments = argumentsIn; // this expects arguments that are parsed in the correct order for use by these methods.
		if(argumentsIn[0].equals("generate")) //if it's generate
			generateExam();
		else if(argumentsIn[0].equals("package")) // or maybe package
			packageExam(argumentsIn[1]);
		else if(argumentsIn[0].equals("test")) // otherwise it MUST be test OR...
			testQuestion();
		else
			error(); // we have a problem.
	}
	
	//generate the exam!!!
	private void generateExam()throws SQLException, IOException
	{
	
		SGServlet sg = new SGServlet();
		
		int[] examID = sg.execute(arguments);
		generateResponse(examID);
	}

	private void packageExam(String examIDIn)throws SQLException, IOException
	{
		SGServlet sg = new SGServlet();
		sg.execute(arguments);
		fileOutName = "ape-" + examIDIn + ".zip";
	}
	
	private void testQuestion()throws SQLException, IOException
	{
		SGServlet sg = new SGServlet();
		System.out.println("Being sent to execute: ");
		for(int i = 0; i < arguments.length; i++)
		{
			System.out.println("Arguments["+i+"] = " + arguments[i]);
		}
		int[] resultsArray = sg.execute(arguments);
		generateResponse(resultsArray);
		
	}
	
	private void error()
	{
	
	}
	
	private void generateResponse(int[] generationDetails)
	{
		fileOutName = "response.html"; //set the name of the html file we will

		//open a printwriter to write the file
		try
		{
		fileOut = new PrintWriter(new File(fileOutName));
		}
		catch(Exception e)
		{
			System.out.println("Could not open the filewriter");
		}
		//write the file
		if(generationDetails[8] < 1) // if this is less than one, then the call is from generate
		
		{
			fileOut.print("<META http-equiv=\"refresh\" content=\"0;URL= " + SITE_BASE_URL + "/?page=preview&exam=" + generationDetails[0] + "\" >");
		}
		else //otherwise, this is from test
		{
			fileOut.print("<META http-equiv=\"refresh\" content=\"0;URL= " + SITE_BASE_URL + "/?page=addQuestionConfirmation&questionID=" + generationDetails[8] + "&questionVersion=" + generationDetails[9] + "&studentPairOne=" + generationDetails[0] + "&studentPairTwo=" + generationDetails[1] + "&studentPairThree=" + generationDetails[2] +  "&studentPairFour=" + generationDetails[3] + "&graderPairOne=" + generationDetails[4] + "&graderPairTwo=" + generationDetails[5] + "&graderPairThree=" + generationDetails[6] + "&graderPairFour=" + generationDetails[7] +"\" >");
		}
		//close the file
		fileOut.close();
		
		
	}
	
	//returns the filename
	public String getFilename()
	{
	
		return fileOutName;
	
	}
}